package com.LC.Controller.SystemManagement;

import com.LC.Constant.MessageConstant;
import com.LC.Entity.*;
import com.LC.Service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {
    
    @Reference
    RoleService roleService;
    
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @PostMapping("/Role/{permissionIds}")
    public Result addRole(@PathVariable Integer[] permissionIds, @RequestBody Role addRole){
        try {
            roleService.addRole(permissionIds,addRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @PutMapping("/Role")
    public Result upDateRole(@RequestBody PUT put){
        try {
            roleService.upDateRole(put.getPermissionIds(), put.getUpdateRole());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    }
    
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("/Role/{id}")
    public Result getRoleById(@PathVariable int id){
        Role roleById = null;
        try {
            roleById = roleService.getRoleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleById);
    }

    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("/Role/Permission/{id}")
    public Result getPermissionIdsByRoleId(@PathVariable int id){
        List<Integer> permissionIdsByRoleId = null;
        try {
            permissionIdsByRoleId = roleService.getPermissionIdsByRoleId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_PERMISSION_LIST_FAIL);
        }
        return new Result(true,MessageConstant.GET_PERMISSION_LIST_SUCCESS,permissionIdsByRoleId);
    }

    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/Role/Page")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
            pageResult = roleService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }
    
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @DeleteMapping("/Role/{id}")
    public Result deleteRoleById(@PathVariable int id){
        try {
            roleService.deleteRole(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }
}
